package com.heyun.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.heyun.bean.HeyunCompManage;
import com.heyun.bean.HeyunEmp;
import com.heyun.bean.HeyunSalesPlan;
import com.heyun.bean.Message;
import com.heyun.service.impl.CompManageServiceImpl;


@Controller
@RequestMapping("/CompManage")
public class CompManageController {
	@Autowired
	private CompManageServiceImpl cmsi;
	
	/*--------------------------------------------1 查找所有数据,并分页展示-----------------------------------------------------------*/
	@RequestMapping("/listCompManage")
	@ResponseBody
	public Map<String, Object> listCompManage(int currentPage) {
		
		Map<String, Object> map = cmsi.listCompManage(currentPage);
		return map;
	}

	/*--------------------------------------------2 添加竞争管理的数据-----------------------------------------------------------*/
	@RequestMapping("/saveCompManage")
	@ResponseBody
	public Message saveCompManage(HeyunCompManage hcm,@RequestParam("datafile") MultipartFile datafile,HttpSession session){
		System.out.println("==============进入竞争管理添加页面==============");
		//1 得到存储数据文件 的真实路径
	    String path=session.getServletContext().getRealPath("comp_manage_datafile");
	    //2 得到文件的上传后的全路径,存储在数据库中
	    String compDataFile=path+"/"+datafile.getOriginalFilename();
	  
	    //3 获取预测生成时间
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String genTime = sdf.format(System.currentTimeMillis());
	
		//4 获取登陆的用户名,作为销售预测的操作人
		HeyunEmp emp=(HeyunEmp) session.getAttribute("user");
		String operator=emp.getEmpRealName();
		
		hcm.setGenTime(genTime);;
		hcm.setOperator(operator);
		hcm.setCompDataFile(compDataFile);
		cmsi.saveCompManage(hcm);
		//上传文件
		try {
			FileUtils.copyInputStreamToFile(datafile.getInputStream(), new File(path+"/"+datafile.getOriginalFilename()));
			//页面重定向
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
	}
	
/*--------------------------------------------3 根据竞争管理的编号查找竞争数据-----------------------------------------------------------*/
	/*
	 * 根据销售计划编号查找本条销售计划的内容
	 */
	@RequestMapping(value = "findCompManageById/{compManageId}", method = RequestMethod.GET)
	@ResponseBody
	public Message findSalesPlanById(
			@PathVariable("compManageId") Integer compManageId) {
		
		HeyunCompManage hcm=cmsi.findCompManageById(compManageId);
		return Message.success().add("hcm", hcm);
	}
	
	/*-----------------------------------------4 根据id修改竞争管理数据-----------------------------------------------------------*/
   @RequestMapping("/updateCompManageById")
		@ResponseBody
		public Message updateCompManageById(HeyunCompManage hcm,HttpSession session) {
			System.out.println("===========修改竞争管理=============");
			System.out.println("salesplanid=========="+hcm.getCompManageId());
			System.out.println("CompResultTitle========="+hcm.getCompResultTitle());
			System.out.println("CompTime========="+hcm.getCompTime());
			System.out.println("CompState==========="+hcm.getCompType());
			 //3 获取预测生成时间
		    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String genTime = sdf.format(System.currentTimeMillis());
		
			//4 获取登陆的用户名,作为销售预测的操作人
			HeyunEmp emp=(HeyunEmp) session.getAttribute("user");
			String operator=emp.getEmpRealName();
		    hcm.setGenTime(genTime);
			hcm.setOperator(operator);
			cmsi.updateCompManageById(hcm);
			return Message.success();
		}
	/*-----------------------------------------5 根据id修改竞争管理数据-----------------------------------------------------------*/
   @RequestMapping(value = "updateCompStateById/{compManageId}", method = RequestMethod.PUT)
 		@ResponseBody
 		public Message updateCompStateById(@PathVariable("compManageId") Integer compManageId ) {
 			
 			
 			cmsi.updateCompStateById(compManageId);
 			return Message.success();
 		}
  
   /*-----------------------------------------6 根据月份查询竞争管理数据-----------------------------------------------------------*/
   @RequestMapping("/findCompManageByMonth")
	@ResponseBody
	public Map<String, Object> findCompManageByMonth(int currentPage,String compTime) {
		System.out.println("查询页数"+currentPage);
		System.out.println("要查询的月份是"+compTime);
		Map<String,Object> map = cmsi.findCompManageByMonth(currentPage, compTime);
		return map;
	}

}
